package work.chenbo.spring_boot_shiro.modules.sys_user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import work.chenbo.spring_boot_shiro.common.util.CodecUtils;
import work.chenbo.spring_boot_shiro.common.vo.PageVo;
import work.chenbo.spring_boot_shiro.common.vo.ResponseObject;
import work.chenbo.spring_boot_shiro.modules.sys_user.constant.ErrorCodeEnum;
import work.chenbo.spring_boot_shiro.modules.sys_user.entity.SysUser;
import work.chenbo.spring_boot_shiro.modules.sys_user.service.SysUserService;
import work.chenbo.spring_boot_shiro.modules.sys_user.vo.BootstrapValidatorVo;
import work.chenbo.spring_boot_shiro.modules.sys_user.vo.UserQO;

/**
 * @author ChenBo
 * @className SysUserController
 * @date 2019/10/30
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class SysUserController {


    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("index")
    public String index(){

        return "user/user-index";
    }


    /**
     * 分页查询数据
     * @author ChenBo
     * @date 2019/10/30
     */
    @RequestMapping("/queryList")
    public @ResponseBody ResponseObject queryList(UserQO qo){
        IPage<SysUser> sysUserIPage = sysUserService.queryList(qo);
        PageVo<SysUser> sysUserPageVo = new PageVo<>();
        sysUserPageVo.setData(sysUserIPage.getRecords());
        sysUserPageVo.setDraw(qo.getDraw());
        sysUserPageVo.setRecordsTotal(sysUserIPage.getTotal());
        return ResponseObject.success(sysUserPageVo);
    }

    /**
     * 页面表单验证用户名是否重复
     * @author ChenBo
     * @date 2019/11/8
     */
    @RequestMapping("/checkUserName")
    public @ResponseBody BootstrapValidatorVo checkUserName(@RequestParam(value = "id",required = false) Long id,@RequestParam("userName")String userName){
        return new BootstrapValidatorVo(sysUserService.checkUserName(id,userName));
    }

    /**
     * 添加用户
     * @author ChenBo
     * @date 2019/11/8
     */
    @RequestMapping("/add")
    public @ResponseBody ResponseObject add(SysUser user){
        if(StringUtils.isNotBlank(user.getUserName()) && StringUtils.isNotBlank(user.getPassword())){
            user.setUserName(CodecUtils.decodeBase64(user.getUserName()));
            user.setPassword(
                    user.getMD5Password(
                            CodecUtils.decodeBase64(user.getPassword()),
                            user.getUserName()
                    )
            );
        }
        int code = sysUserService.add(user);
        if(ErrorCodeEnum.SUCCESS.getCode().equals(code)){
            return ResponseObject.success();
        }else {
            return ResponseObject.fail(code, ErrorCodeEnum.getMsg(code));
        }
    }

    @RequestMapping("/queryOne")
    public @ResponseBody ResponseObject queryOne(SysUser user){
        if(user.getId()==null){
            log.error("获取用户请求参数错误,用户ID为空");
            return ResponseObject.fail(500,"系统错误");
        }
        SysUser sysUser = sysUserService.queryOne(user);
        if(sysUser==null){
            log.error("主键[{}]获取用户为空",user.getId());
           return ResponseObject.fail(600,"系统错误没有找到用户");
        }
        return ResponseObject.success(sysUser);
    }

    /**
    * 修改用户
    * @author; ChenBo
    * @datetime: 2019/11/13
    */
    @RequestMapping("/edit")
    public @ResponseBody ResponseObject edit(SysUser user){
        int update = sysUserService.update(user);
        if(update>0){
           return ResponseObject.success();
        }else {
           return ResponseObject.fail(5643,"修改失败");
        }
    }



}
